package com.k136.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.k136.common.RedisKey;
import com.k136.dao.BrandDao;
import com.k136.entity.Add;
import com.k136.entity.Brand;
import com.k136.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * TODO 类
 *
 * @author LiuTai
 * @version 1.00
 * @Date 2024/8/28
 */
@Service
public class BrandServiceImpl  implements BrandService {
    @Resource
    private BrandDao brandDao;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public List<Brand> selBrand() {
        ValueOperations<String,Object> opsForValue=redisTemplate.opsForValue();
        Object selBrandObj=opsForValue.get(RedisKey.Brand_List);
        List<Brand> brandList=null;
        if (selBrandObj == null){
            System.out.println("数据库查询");
            brandList=brandDao.selectBrand();
            opsForValue.set(RedisKey.Brand_List,brandList,5, TimeUnit.MINUTES);
        }else {
            System.out.println("缓存中获取数据");
            brandList= JSONObject.parseArray(selBrandObj.toString(),Brand.class);
        }
        return brandList;
    }
}
